﻿using AppSocketModel;
using AppSocketService.Device;
using BCGPSCommon;
using BCGPSMongoData;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace AppSocketControl.GpsData.DataDispatch
{
    /// <summary>
    /// 处理设备运行状态数据帧
    /// </summary>
    public class DispatchRunStatus
    { 
        private readonly MongoDBCommand< DeviceRunStatus>  mongoDBCommand;
        private  DeviceRunStatus deviceRunStatus = new DeviceRunStatus ();
        private DeviceDto device = new DeviceDto();
        private IDeviceService deviceService = new DeviceService(new RedisService());
        public DispatchRunStatus(  )
        {
            mongoDBCommand = new MongoDBCommand< DeviceRunStatus>("DeviceRunStatus");
 
        }

        private  void Split(string[] dataArr)
        {
            deviceRunStatus = new DeviceRunStatus();
            deviceRunStatus.HardwareVersion = dataArr[1];
            deviceRunStatus.HardwareCode = dataArr[2];
            deviceRunStatus.FirmwareVersion = dataArr[3];
            deviceRunStatus.FirmwareCode = dataArr[4];
            deviceRunStatus.IMEI = dataArr[5];
            Msg.WirteLine(deviceRunStatus.IMEI, 10);
            deviceRunStatus.DeviceInfo = dataArr[7];
            deviceRunStatus.IP = dataArr[9];
            int port = 0;
            if (int.TryParse(dataArr[10], out port))
                deviceRunStatus.Port = port;
            else
                return;
            deviceRunStatus.ReceiveDateTime = MongoDBDateTime.GetNowUtc();

            if (deviceRunStatus.HardwareCode.ToUpper().Contains("BAT-4.2V"))
                deviceRunStatus.deviceType = DeviceType.BAT42;
            if (deviceRunStatus.HardwareCode.ToUpper().Contains("BAT-3V"))
                deviceRunStatus.deviceType = DeviceType.BAT30;
            else
                deviceRunStatus.deviceType = DeviceType.VBS;


            var arr2 = dataArr[7].Split(',');
            if(arr2.Length > 0)
            {
                foreach (var item in arr2)
                {
                    Msg.WirteLine(item, 10);
                    string[] itemArr = Regex.Split(item, ":=", RegexOptions.IgnoreCase);
                    if(itemArr.Length > 1)
                    { 
                        try
                        {
                            switch (itemArr[0])
                            {
                                case "1":
                                case "ResetCount":
                                    deviceRunStatus.ResetCount = int.Parse(itemArr[1]);
                                    break;
                                case "2":
                                case "SendTimeSpan":
                                    deviceRunStatus.TimeSpan = int.Parse(itemArr[1]);
                                    break;
                                case "3":
                                case "GprsModule":
                                    deviceRunStatus.GprsModule = itemArr[1];
                                    break;
                                case "4":
                                case "BatVoltage":
                                    deviceRunStatus.BatteryVoltage = float.Parse( itemArr[1]);
                                    break;
                                case "5":
                                case "WkupType":
                                    deviceRunStatus.WakeUpType = itemArr[1];
                                    break;
                                case "6":
                                case "SleepTime":
                                    deviceRunStatus.SleepTime = int.Parse(itemArr[1]);
                                    break;
                                case "7":
                                case "SendTimes":
                                    deviceRunStatus.SendTimes = int.Parse(itemArr[1]);
                                    break;
                                case "8":
                                case "SleepTimeOnErr":
                                    deviceRunStatus.SleepTimeOnErr = int.Parse(itemArr[1]);
                                    break;
                                case "9":
                                case "Len":
                                    deviceRunStatus.Len = int.Parse(itemArr[1]);
                                    break;
                                case "10":
                                case "ResetTotal":
                                    deviceRunStatus.ResetTotal = int.Parse(itemArr[1]);
                                    break;
                                case "11":
                                case "ICCID":
                                    deviceRunStatus.ICCID = itemArr[1];
                                    break;
                                default:
                                    break;
                            }  
                        }
                        catch (System.Exception ex)
                        {
                            var msg = ex.Message;
                        }
                    }
                } 
            }
        }
        /// <summary>
        /// 设备运行状态数据写入数据库
        /// </summary>
        /// <param name="dataArr"></param>
        public void Run(string[] dataArr)
        {
            Split(dataArr);
            mongoDBCommand.InsertDocumentAsync(deviceRunStatus).GetAwaiter().GetResult();

            device = new DeviceDto();
            device.IMEI = deviceRunStatus.IMEI;
            device.ICCID = deviceRunStatus.ICCID;
            device.FirmwareCode = deviceRunStatus.FirmwareCode;
            device.FirmwareVersion = deviceRunStatus.FirmwareVersion;
            device.HardwareCode = deviceRunStatus.HardwareCode;
            device.HardwareVersion = deviceRunStatus.HardwareVersion;
            device.deviceType = deviceRunStatus.deviceType;
            device.ResetCount = deviceRunStatus.ResetCount;
            device.ResetTotal = deviceRunStatus.ResetTotal;
            device.TimeSpan = deviceRunStatus.TimeSpan;
            device.SleepTime = deviceRunStatus.SleepTime;
            device.SleepTimeOnErr = deviceRunStatus.SleepTimeOnErr;
            device.SendTimes = deviceRunStatus.SendTimes;
            device.GprsModule = deviceRunStatus.GprsModule;
            device.BatteryVoltage = deviceRunStatus.BatteryVoltage;
            device.WakeUpType = deviceRunStatus.WakeUpType;
            deviceService.Login(device);
         
        }
    }
}
